﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComicBookApp.Data.DataAdapters.Interfaces;
using ComicBookApp.DataModels;

namespace ComicBookApp.Data.DataAdapters.Adapters
{
    public class ComicBookAppAdapter : IComicBookAppAdapter
    {

        public DataModels.ComicBook CreateComicBook(DataModels.ComicBook comicBook)
        {
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                comicBook = db.ComicBooks.Add(comicBook);
                db.SaveChanges();
            }
            return comicBook;
        }

        public List<DataModels.ComicBook> GetAllComicBooks()
        {
            List<ComicBook> comics;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                comics = db.ComicBooks.OrderBy(x => x.Title).ToList();
            }

            return comics;
        }

        public List<ComicBook> GetComicsForCreator(Creator creator)
        {
            List<ComicBookCreator> cbc;
            List<ComicBook> comics = new List<ComicBook>();
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                cbc = db.ComicBookCreators.Where(x => x.CreatorId == creator.Id).ToList();
                foreach(var c in cbc)
                {
                    comics.Add(db.ComicBooks.Find(c.ComicBookId));
                }
            }
            return comics;
        }

        public DataModels.ComicBook GetComicBook(int id)
        {
            ComicBook comic;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                comic = db.ComicBooks.Find(id);
            }
            return comic;
        }

        public DataModels.ComicBook UpdateComicBook(DataModels.ComicBook comicBook)
        {
            ComicBook dbComic;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbComic = db.ComicBooks.Find(comicBook.Id);
                dbComic.SetProperties(comicBook);
                db.SaveChanges();
            }
            return dbComic;
        }

        public DataModels.ComicBook DeleteComicBook(int id)
        {
            ComicBook comic;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                comic = db.ComicBooks.Find(id);
                List<ComicBookCreator> cbc = db.ComicBookCreators.Where(x => x.ComicBookId == comic.Id).ToList();
                db.ComicBookCreators.RemoveRange(cbc);
                db.ComicBooks.Remove(comic);
                db.SaveChanges();
            }
            return comic;
        }

        public DataModels.Character CreateCharacter(DataModels.Character character)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Add(character);
                db.SaveChanges();
            }
            return character;

        }

        public List<DataModels.Character> GetAllCharacters()
        {
            List<Character> characters;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                characters = db.Characters.OrderBy(x => x.Name).ToList();
            }
            return characters;
        }

        public DataModels.Character GetCharacter(int id)
        {
            Character character;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Find(id);
            }
            return character;
        }

        public List<Character> GetCharactersForComic(ComicBook comic)
        {
            List<Character> characters = new List<Character>();
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                characters = db.Characters.Where(x => x.ComicBookId == comic.Id).ToList();
            }
            return characters;
        }

        public DataModels.Character UpdateCharacter(DataModels.Character character)
        {
            Character dbCharacter;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbCharacter = db.Characters.Find(character.Id);
                dbCharacter.SetProperties(character);
                db.SaveChanges();
            }
            return dbCharacter;
        }

        public DataModels.Character DeleteCharacter(int id)
        {
            Character character;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                character = db.Characters.Find(id);
                db.Characters.Remove(character);
                db.SaveChanges();
            }
            return character;
        }

        public DataModels.Creator CreateCreator(DataModels.Creator creator)
        {
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                creator = db.Creators.Add(creator);
                db.SaveChanges();
            }
            return creator;
        }

        public List<DataModels.Creator> GetAllCreators()
        {
            List<Creator> creators;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                creators = db.Creators.OrderBy(x => x.Name).ToList();
            }
            return creators;
        }

        public DataModels.Creator GetCreator(int id)
        {
            Creator creator;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                creator = db.Creators.Where(x => x.Id == id).FirstOrDefault();
            }
            return creator;
        }

        public List<Creator> GetCreatorsForComic(ComicBook comic)
        {
            List<Creator> creators = new List<Creator>();
            List<ComicBookCreator> cbc;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                cbc = db.ComicBookCreators.Where(x => x.ComicBookId == comic.Id).ToList();
                foreach (var c in cbc)
                {
                    creators.Add(db.Creators.Find(c.CreatorId));
                }
            }
            return creators;
        }

        public DataModels.Creator UpdateCreator(DataModels.Creator creator)
        {
            Creator dbCreator;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbCreator = db.Creators.Find(creator.Id);
                dbCreator.SetProperties(creator);
                db.SaveChanges();
            }
            return dbCreator;
        }

        public DataModels.Creator DeleteCreator(int id)
        {
            Creator creator;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                creator = db.Creators.Find(id);
                List<ComicBookCreator> cbc = db.ComicBookCreators.Where(x => x.CreatorId == creator.Id).ToList();
                db.ComicBookCreators.RemoveRange(cbc);
                db.Creators.Remove(creator);
                db.SaveChanges();
            }
            return creator;
        }

        public Creator CheckForCreator(string name)
        {
            Creator creator;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                creator = db.Creators.Where(x => x.Name == name).FirstOrDefault();
            }
            return creator;
        }
        
        public ComicBookCreator CreateComicBookCreator(ComicBookCreator comicCreator)
        {
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                comicCreator = db.ComicBookCreators.Add(comicCreator);
                db.SaveChanges();
            }
            return comicCreator;
        }

        public void BreakComicBookCreatorLink(ComicBook comic, Creator creator)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                ComicBookCreator cbc = db.ComicBookCreators.Where(x => x.ComicBookId == comic.Id && x.CreatorId == creator.Id).FirstOrDefault();
                db.ComicBookCreators.Remove(cbc);
                db.SaveChanges();
            }

        }
        
    }
}
